package com.le.tester.book.concurrentchallenge.synchronizedUtil14;

import com.aliyuncs.http.HttpResponse;

import javax.print.attribute.standard.OrientationRequested;

/**
 * createTime：2025-05-20 11:13
 * description：有界缓存实现的基类
 */
public abstract class BaseBoundedBuffer<V> {
    private final V[] buf;

    private int tail;

    private int head;

    private int count;

    public BaseBoundedBuffer(int size) {
        this.buf = (V[]) new Object[size];
    }

    protected synchronized final void doPut(V v) {
        buf[tail] = v;
        if (++tail == buf.length) {
            tail = 0;
        }
        ++count;
    }

    protected synchronized final V doTake() {
        V v = buf[head];
        buf[head] = null;

        if (++head == buf.length) {
            head = 0;
        }
        --count;
        return v;
    }

    public synchronized final boolean isFull() {
        return count == buf.length;
    }

    public synchronized final boolean isEmpty() {
        return count == 0;
    }
}
